package leetcodecompetition.S112;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Siantar
 * @Date: 2023-09-02-22:51
 * @Description: 1.0
 */
public class T6989 {
    public long maxSum(List<Integer> nums, int m, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        long sum = 0,res = 0;
        int s = 0;
        for (int i = 0; i < k-1; i++) {
            int num = nums.get(i);
            sum += num;
            if (s >= m) {
                res = Math.max(res, sum);
            }
            if (!map.containsKey(num)) {
                s++;
            }
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        for (int i = k-1; i < nums.size(); i++) {
            int num = nums.get(i);
            sum += num;
            if (s >= m) {
                res = Math.max(res, sum);
            }
            if (!map.containsKey(num)) {
                s++;
            }
            map.put(num, map.getOrDefault(num, 0) + 1);
            System.out.println(num+" "+s+" "+sum+" ");
            int leftNum = nums.get(i - k+1);
            sum -= leftNum;
            int cnt = map.get(leftNum);
            if (cnt == 1) {
                map.remove(leftNum);
                s--;
            } else {
                map.put(leftNum, cnt - 1);
            }
        }
        return res;
    }

}
